package servlet;

import dao.Db;
import javabean.Worker;
import ulity.DBConn;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

@WebServlet("/worker")
public class ShowList extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受用户请求，访问业务逻辑层，获取表中所有数据，并且渲染成表格
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        Db db = new Db();
        List<Worker> allDate = db.getAllDate();

        //把获取到的数据渲染成列表返回给前端
        PrintWriter writer = resp.getWriter();
        writer.write("<!doctype html>");
        writer.write("<html>");
        writer.write("<head><title>员工信息列表展示</title><meta charset='utf-8'></head>");
        writer.write("<body style='background:lightblue;'>");
        writer.write("<table border='1' cellpadding='1' width='800' align='center' cellspcing='1'>");
        writer.write("<tr><th>id</th><th>姓名</th><th>工作</th><th>操作1</th><th>操作2</th></tr>");
        for (int i = 0; i < allDate.size(); i++) {
            writer.write("<tr><td>"+allDate.get(i).getId()+"</td>");
            writer.write("<td>"+allDate.get(i).getName()+"</td>");
            writer.write("<td>"+allDate.get(i).getWork()+"</td>");
            writer.write("<td><a href=/JavaWebStudy_war_exploded/delete?id="+allDate.get(i).getId()+" onclick=\"if(confirm('确定删除吗？')) return true;return false;\">删除</a></td>");
            writer.write("<td><a href=/JavaWebStudy_war_exploded/getOne?id="+allDate.get(i).getId()+">更新</a></td></tr>");
        }
        writer.write("</table>");
        writer.write("</body>");
        writer.write("</html>");
    }
}
